package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.Address;
import com.sxbbc.common.core.mapper.jz.AddressMapper;
import com.sxbbc.common.core.service.IAddressService;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 全球地区库，采集自腾讯QQ国内+国际版.ADD.JENA.20141221 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-04-29
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    public List<Address> queryAddressByParentId(Address address) {
        QueryWrapper<Address> wrapper = queryWrapper(address);
        List<Address> list = super.list(wrapper);
        return list;
    }

    @Override
    public Page<Address> queryAddressPage(BasePage<Address> page, Address address) {
        QueryWrapper<Address> wrapper = queryWrapper(address);
        if (page.isPaging()) {
            super.page(page, wrapper);
        } else {
            List<Address> list = super.list(wrapper);
            page.setRecords(list);
        }
        return page;
    }

    @Override
    public List<Address> queryAllAddress() {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",StaticUtils.ADDRESS_COUNTRY);
        wrapper.orderByAsc("sort");
        List<Address> list = super.list(wrapper);
        for (Address address : list) {
            QueryWrapper<Address> cityWr = new QueryWrapper<>();
            cityWr.eq("pid",address.getId());
            List<Address> city = super.list(cityWr);
            if (!CollectionUtils.isEmpty(city) && city.size() > 0) {
                address.setCityList(city);
            }
        }
        return list;
    }

    @Override
    public String getAddrName(Integer proId, Integer cityId, Integer areaId) {
        StringBuffer name = new StringBuffer();
        Address address = super.getById(proId);
        if (address != null) {
            name = name.append(address.getName());
        }
        address = super.getById(cityId);
        if (address != null) {
            name = name.append(" ").append(address.getName());
        }
        address = super.getById(areaId);
        if (address != null) {
            name = name.append(" ").append(address.getName());
        }
        return name.toString();
    }
    
    private QueryWrapper<Address> queryWrapper(Address address) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        CalcuUtils cal = CalcuUtils.getInstance();
        // 父id非空取父id，为空取0即中国范围内的省
        if (address.getPid() != null) {
            wrapper.eq("pid",address.getPid());
        }else{
            wrapper.eq("pid",StaticUtils.ADDRESS_COUNTRY);
        }
        if (address.getLevel() != null) {
            wrapper.eq("level",address.getLevel());
        }
        if (cal.isEquals(address.getStatus(), StaticUtils.STATUS_NO)) {
            wrapper.eq("status", address.getStatus());
        } else if (cal.isEquals(address.getStatus(), StaticUtils.STATUS_YES)) {
            wrapper.eq("status", StaticUtils.STATUS_YES);
        } else {
            wrapper.eq("status", StaticUtils.STATUS_YES);
        }
        return wrapper;
    }
}
